import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

//队列实现栈
class MyStack {
      protected Queue<Integer> queue1;
      protected Queue<Integer> queue2;
    public MyStack() {
     queue1=new LinkedList<>();//入队
     queue2=new LinkedList<>();
    }
    public void push(int x) {
        if(empty()) {
            queue1.offer(x);
        }else{
            if(!queue1.isEmpty()){
                queue1.offer(x);
            }else{
                queue2.offer(x);
            }
        }
    }
    
    public int pop() {
        if(empty()){
            return -1;
        }else if (!queue1.isEmpty()){
                int len=queue1.size();
                for (int i = 0; i <len-1 ; i++) {
                    queue2.offer(queue1.poll());
                }
                return queue1.poll();
            }else{
            int len=queue2.size();
            for (int i = 0; i <len-1 ; i++) {
                queue1.offer(queue2.poll());
            }
               return queue2.poll();
            }
        }
    public int top() {
        int ret=-1;
        if(empty()){
            return -1;
        }else if(!queue1.isEmpty()){
            int len=queue1.size();
            for (int i = 0; i <len ; i++) {
                ret=queue1.poll();
                queue2.offer(ret);
            }
            return ret;
        }else{
            int len=queue2.size();
            for (int i = 0; i <len ; i++) {
                ret=queue2.poll();
                queue1.offer(ret);
            }
            return ret;
        }

    }
    
    public boolean empty() {
        if(queue1.isEmpty()&&queue2.isEmpty()){
            return true;
        }
      return false;
    }
}
